package com.first.controller;


import com.first.dao.MenuDao;
import com.first.entity.Menu;
import com.first.entity.User;
import com.first.util.DBUtil;
import com.first.util.Result;
import com.first.util.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import sun.security.pkcs11.Secmod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuDao menuDao;
    /**
     *通过登录用户的rid获取用户的权限
     * @param request
     * @return
     */
    @RequestMapping("getAllMenuById")
    public ModelAndView getAllMenuById(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        User user = (User) request.getSession().getAttribute("user");
        List<Menu> menu = menuDao.getAllMenuById(user.getRid());
        mv.setViewName("homePage");
        mv.addObject("menu",menu);
        return mv;
    }
    /**
     * 获取全部权限信息，并返回tree 格式数据
     * @return
     */
    @RequestMapping("getAllMenu")
    public List<Tree> getAllMenu(){
        List<Tree> allMenu = menuDao.getAllMenu();
        return allMenu;
    }

    /**
     * 通过选中行的id获取对应的权限
     * @param rid
     * @return
     */
    @RequestMapping("getAllSonMenuMidByRid")
    public List getAllsonMenuById(Integer rid){
        List menuById = menuDao.getAllsonMenuById(rid);
        return menuById;
    }
    /**
     * 通过rid,修改前台传来的权限信息
     * @param rid
     * @param midArr
     * @return
     */
    @RequestMapping("editPer")
    public Result editPer(Integer rid,Integer[] midArr ){
        int i = menuDao.editPer(rid, midArr);
        Result result = new Result();
        if(i>0){
            result.setCode(100);
            result.setMsg("修改成功");
        }else {
            result.setCode(400);
            result.setMsg("修改失败");
        }
        return result;
    }
}
